#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <signal.h>
#include <linux/kd.h>
#include <sys/ioctl.h>

void slam_alarm_handler(int a)
{
  int led = 0;
  int tty = 0;

  tty = open("/dev/console", 0);
  if (tty < 3) {
    perror("open:");
    exit(0);
  }

  led = 0x7;
  if (ioctl(tty, KDSETLED, led) > 0)
    perror("ioctl led on:");
  sleep(1);

  led = 0xff;
  if (ioctl(tty, KDSETLED, led) > 0)
    perror("ioctl led off:");

  close(tty);
}

int main(void)
{
  /*
   * The struct itimerval
   * it_interval:means interval everytime after first time
   * it_value:means the first time interval
   * the ITIMER_REAL timer come with SIGALRM signal
   */

  struct itimerval t;
  t.it_interval.tv_usec = 0;
  t.it_interval.tv_sec = 2;
  t.it_value.tv_usec = 0;
  t.it_value.tv_sec = 2;
  if (setitimer(ITIMER_REAL, &t, NULL) < 0) {
    printf("setitimer failed.\n");
    exit(-1);
  }

  signal(SIGALRM, slam_alarm_handler);

  while(1) {
    sleep(2);
  }

  exit(0);
}
